package am.NotenProgramm.Frame.Panels;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import am.NotenProgramm.Frame.GradePro;

public class SettingsPanel extends JPanel implements ActionListener{
	
	JPanel leftPanel = new JPanel();
	JPanel rightPanel = new JPanel();
	
	String[] languagesShort = {"en", "de"};
	String[] languagesFull;
	JComboBox comboBoxLanguages;

	String[] profileNames = {""};
	JComboBox profileNamesComboBox;
	
	JTextField textFieldSizeX = new JTextField();
	JTextField textFieldSizeY = new JTextField();
	
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Dimension scrnsize = toolkit.getScreenSize();
	
	JButton buttonAccept = new JButton(GradePro.rb.getString("accept"));
	JButton buttonAbort = new JButton(GradePro.rb.getString("abort"));
	
	public SettingsPanel(Dimension size) {
		setSize(size);
		initUI();
		//setBackground(Color.BLUE);
		//add(new JLabel("Under Construction"));
	}
	
	private void initUI() {
		languagesFull = new String[languagesShort.length];
		for(int i = 0; i < languagesShort.length; i++) {
			languagesFull[i] = GradePro.rb.getString(languagesShort[i]);
		}
		comboBoxLanguages = new JComboBox(languagesFull);
		
		profileNames = GradePro.myStream.getProfileNames();
		profileNamesComboBox = new JComboBox(profileNames);
		for(int i = 0; i < profileNames.length; i++) {
			if(profileNames[i].equals(GradePro.mySettings.getStandartProfileName())) {
				profileNamesComboBox.setSelectedIndex(i);
				break;
			}
		}
		profileNamesComboBox.setEditable(true);
		
		
		buttonAbort.addActionListener(this); buttonAbort.setActionCommand("abort");
		buttonAccept.addActionListener(this); buttonAccept.setActionCommand("accept");
		
		for(int i = 0; i < languagesShort.length; i++) {
			if(languagesShort[i].equals(GradePro.mySettings.getLanguage())) {
				comboBoxLanguages.setSelectedIndex(i);
				break;
			}
		}
		textFieldSizeX.setText(""+GradePro.mySettings.getWidth());
		textFieldSizeY.setText(""+GradePro.mySettings.getHeight());
		
		rightPanel.setLayout(new GridLayout(6, 1));
		leftPanel.setLayout(new GridLayout(6, 1));
		
		setLayout(null);
		
		add(rightPanel);
		rightPanel.setLocation(this.getSize().width/2+10, 10);
		rightPanel.setSize(this.getSize().width/2-20, 180);
		
		
		add(leftPanel);
		leftPanel.setLocation(10, 10);
		leftPanel.setSize(this.getSize().width/2-20, 180);
		
		
		leftPanel.add(new JLabel(GradePro.rb.getString("language")));
		rightPanel.add(comboBoxLanguages);
		
		leftPanel.add(new JLabel(GradePro.rb.getString("profileName")));
		rightPanel.add(profileNamesComboBox);

		leftPanel.add(new JLabel("X-" + GradePro.rb.getString("size")));
		rightPanel.add(textFieldSizeX);
		
		leftPanel.add(new JLabel("Y-" + GradePro.rb.getString("size")));
		rightPanel.add(textFieldSizeY);
		
		leftPanel.add(buttonAbort);
		rightPanel.add(buttonAccept);
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("abort")){
			for(int i = 0; i < languagesShort.length; i++) {
				if(languagesShort[i].equals(GradePro.mySettings.getLanguage())) {
					comboBoxLanguages.setSelectedIndex(i);
					break;
				}
			}
			textFieldSizeX.setText(""+GradePro.mySettings.getWidth());
			textFieldSizeY.setText(""+GradePro.mySettings.getHeight());
		}
		if(e.getActionCommand().equals("accept")){
			try {
				if(Integer.parseInt(textFieldSizeX.getText()) > scrnsize.width){
					textFieldSizeX.setText(""+scrnsize.width);
				} else if(Integer.parseInt(textFieldSizeX.getText()) < 800) {
					textFieldSizeX.setText("800");
				}
				if(Integer.parseInt(textFieldSizeY.getText()) > scrnsize.height){
					textFieldSizeY.setText(""+scrnsize.height);
				} else if(Integer.parseInt(textFieldSizeY.getText()) < 600) {
					textFieldSizeY.setText("600");
				}
				GradePro.mySettings.setWidth(Integer.parseInt(textFieldSizeX.getText()));
				GradePro.mySettings.setHeight(Integer.parseInt(textFieldSizeY.getText()));
				GradePro.mySettings.setStandartProfileName(profileNamesComboBox.getSelectedItem().toString());
				GradePro.mySettings.setLanguage(languagesShort[comboBoxLanguages.getSelectedIndex()]);
				GradePro.myStream.saveSettings(GradePro.mySettings);
				GradePro.restart();
				this.setVisible(false);
				GradePro.mainFrame.remove(this);
			} catch(Exception e1) {
				textFieldSizeX.setText(""+GradePro.mySettings.getWidth());
				textFieldSizeY.setText(""+GradePro.mySettings.getHeight());
			}
		}
	}

}
